package com.qiall.omsserver.service.dubbo.findService;


import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 吴金辉
 */
@Component
public class Context implements InitializingBean , ApplicationContextAware {

    private ApplicationContext applicationContext;
    public Map<StrategyEnum, FindDubboService> strategyEnumFindDubboServiceMap=new HashMap<>();

    @Override
    public void afterPropertiesSet() {
        Map<String, FindDubboService> applicationContextBean = applicationContext.getBeansOfType(FindDubboService.class);
        applicationContextBean.entrySet().forEach(x -> {
            strategyEnumFindDubboServiceMap.put(x.getValue().getStrategy(), x.getValue());
        });
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    public FindDubboService getService(String code){
        StrategyEnum strategyEnum= StrategyEnum.ofCode(code);
        return strategyEnumFindDubboServiceMap.get(strategyEnum);
    }
}
